import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart';
import '../../../app_files.dart';
import 'action.dart';
import 'state.dart';
import '../widget/main_item_widget.dart';
import '../widget/check_list_widget.dart';

Widget buildView(
    CheckListViewState state, Dispatch dispatch, ViewService viewService) {
  const List<Map> statuesOne = [
    {'name': '全部', 'value': '0'},
    {'name': '安检中', 'value': '4'},
    {'name': '未入户', 'value': '1'},
    {'name': '到访不遇', 'value': '2'},
    {'name': '拒绝安检', 'value': '3'},
  ];
  const List<Map> statuesTwo = [
    {'name': '全部', 'value': '0'},
    {'name': '正常', 'value': '1'},
    {'name': '自修', 'value': '2'},
    {'name': '隐患', 'value': '3'},
    {'name': '维修', 'value': '4'},
  ];

  List<Map> getSortList() => state.checkStatus == 1 ? statuesOne : statuesTwo;
  Widget _sortWidget() {
    return Container(
      height: 46,
      margin: EdgeInsets.only(top: 0),
      width: double.infinity,
      color: Colors.white,
      child: Row(
        children: [
          InkWell(
            child: Container(
              padding: EdgeInsets.symmetric(horizontal: 24, vertical: 10),
              child: Text(
                '待安检',
                style: state.checkStatus == 1
                    ? TextThemeConfig.Font15WithMainColor
                    : TextThemeConfig.Font15WithColor99,
              ),
            ),
            onTap: () => dispatch(CheckListViewActionCreator.onTapSort(1)),
          ),
          InkWell(
            child: Container(
              padding: EdgeInsets.symmetric(horizontal: 24, vertical: 10),
              child: Text(
                '已安检',
                style: state.checkStatus == 2
                    ? TextThemeConfig.Font15WithMainColor
                    : TextThemeConfig.Font15WithColor99,
              ),
            ),
            onTap: () => dispatch(CheckListViewActionCreator.onTapSort(2)),
          ),
        ],
      ),
    );
  }

  Widget _tabWidget() {
    return Container(
      height: 45,
      decoration: BoxDecoration(
        color: Colors.white,
        border:
            Border(top: BorderSide(color: ThemeConfig.separeColor, width: 1)),
      ),
      child: Row(
        children: [
          Expanded(
            child: H_TabBar(
              tabs: getSortList().map<H_Tab>((Map data) {
                return H_Tab(
                  text: data['name'],
                );
              }).toList(),
              labelColor: Color(0xff4D81EF),
              labelStyle: TextStyle(
                  fontSize: 14,
                  fontWeight: FontWeight.w600,
                  textBaseline: TextBaseline.ideographic),
              unselectedLabelColor: Color(0xff999999),
              unselectedLabelStyle: TextStyle(
                  fontSize: 14,
                  fontWeight: FontWeight.w500,
                  textBaseline: TextBaseline.ideographic),
              labelPadding: EdgeInsets.symmetric(horizontal: 11),
              indicator: UnderlineTabIndicator(
                borderSide: BorderSide(color: Color(0xff4D81EF), width: 2),
                insets: EdgeInsets.symmetric(horizontal: 16),
              ),
              isScrollable: state.checkStatus == 1 ? true : false,
            ),
          ),
        ],
      ),
    );
  }

  return Scaffold(
    appBar: myAppBar(viewService.context, title: state.title),
    body: DefaultTabController(
      length: state.checkStatus == 1 ? 5 : 5,
      child: Column(
        key: state.scaffoldKey,
        children: [
          Container(
            color: ThemeConfig.mainColor,
            child: MySearchWidget(
              searchText: '搜索户名/联系方式/楼号',
              heroTag: 'checkSearch',
              type: 2,
              communityID: state.itemID,
              communityName: state.title,
              planID: state.planID,
            ),
          ),
          _sortWidget(),
          _tabWidget(),
          Expanded(
            child: TabBarView(
                children: getSortList().map<Widget>((Map model) {
              CheckListModel listModel = state.listModels[model['value']];
              if (listModel == null) {
                listModel = CheckListModel();
                state.listModels[model['value']] = listModel;
              }
              return Container(
                padding: EdgeInsets.only(top: 6),
                child: CheckListWidget(
                  model: listModel,
                  tag: model['value'],
                  onLoad: (String tag, int page) =>
                      Future.delayed(Duration(milliseconds: 300)).then(
                          (value) => dispatch(
                              CheckListViewActionCreator.onLoad(page, tag))),
                ),
              );
            }).toList()),
          ),
        ],
      ),
    ),
  );
}
